package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.MenuService;
import com.itheima.service.RoleService;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenuService menuService;
    @Autowired
    private UserService userService;

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //调用dao方法
        roleDao.add(role);
        //自定义添加角色和权限的级联组
        setRoleAndPermission(role.getId(),permissionIds);
        //自定义添加角色和菜单的级联组
        setRoleAndMenu(role.getId(),menuIds);
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findPermissisonsByUserId(Integer id) {
        return roleDao.findPermissisonsByUserId(id);
    }

    @Override
    public List<Integer> findMenusByUserId(Integer id) {
        return roleDao.findMenusByUserId(id);
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.deletePermissionAssociation(role.getId());
        roleDao.deleteMenuAssociation(role.getId());
        setRoleAndPermission(role.getId(),permissionIds);
        setRoleAndMenu(role.getId(),menuIds);
        roleDao.edit(role);
        List<User> users = userService.userList();
        for (User user : users) {
            menuService.getAllMenusForCurrentUser(user.getUsername());
        }
    }

    @Override
    public void delete(Integer id) {
        roleDao.deletePermissionAssociation(id);
        roleDao.deleteMenuAssociation(id);
        roleDao.delete(id);
        List<User> users = userService.userList();
        for (User user : users) {
            menuService.getAllMenusForCurrentUser(user.getUsername());
        }
    }

    //自定义添加角色和权限的级联组
    public void setRoleAndPermission(Integer roleId,Integer[] permissionIds){
        if(permissionIds !=null && permissionIds.length>0){
            for(Integer permissionId : permissionIds){
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("permission_id",permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }


    //自定义添加角色和权限的级联组
    public void setRoleAndMenu(Integer roleId,Integer[] menuIds){
        if(menuIds !=null && menuIds.length>0){
            for(Integer menuId : menuIds){
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("menu_id",menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
        List<User> users = userService.userList();
        for (User user : users) {
            menuService.getAllMenusForCurrentUser(user.getUsername());
        }
    }


}
